﻿using FileData.Currency;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FileData.DataUtils.CurrentlyUtils;

namespace FileData.DataUtils
{
    class RestoreData
    {
        /// <summary>
        /// 还原一个文件夹下所有文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public void RestoreFloder(string FloderPath, bool RemoveImgInfo) {
            if (FloderPath == "")
                return;
            List<string> AllFile = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string Line in AllFile)
            {
                string FileExt = new FileNameHelper().GetFileType(Line);
                if (FileExt != ".lst" && FileExt != ".txt")
                {
                    Console.WriteLine("{0}文件类型不符合，不缩短。（仅缩短.lst和.txt文件）", Line);
                    continue;
                }
                RestoreSingleFile(Line, RemoveImgInfo, false);
            }
         
                new MessageHelper().OpenFloderAlert(FloderPath);

        }
        public void RestoreSingleFile(string FilePath,bool RemoveImgInfo,bool AlertFlage=true) {

            string ShortInfoFile = new CurrentlyUtils().GetRuleFile(FilePath);
            if (ShortInfoFile == "")
                return;
            List<ShortDataInfo> ReplaceTextList = new CurrentlyUtils().ReadDataInfo(ShortInfoFile);
            RestoreFile(FilePath, ReplaceTextList, RemoveImgInfo);
            if (AlertFlage)
                new MessageHelper().OpenFloderAlert(new FloderNameHelper().GetFloderPath(FilePath));
        }
        /// <summary>
        /// 恢复文件关键信息 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="replaceTextList"></param>
        private void RestoreFile(string filePath, List<ShortDataInfo> ReplaceTextList,bool RemoveImgInfo)
        {
            List<string> TempList = FileHelper.ReadAllLine(filePath);

            foreach (ShortDataInfo SinleData in ReplaceTextList)
            {

                TempList = new CurrentlyUtils().RepListHelper(TempList, SinleData.ReplaceTo, SinleData.ReplaceForm);
            }

            if (RemoveImgInfo)
                TempList = new ListHelper().ReplaceList(TempList, "\\(.*?\\)");

            FileHelper.WriteUTF8List(new FileNameHelper().CreateFileNameAddTip(filePath, "_Restore"), TempList);
        }
    }
}
